package com.ac.controller;

import java.util.List;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ac.aop.Auth;
import com.ac.model.AuthApi;
import com.ac.response.Res;
import com.ac.service.AuthApiService;
import com.ac.service.AuthTokenService;
import com.ac.util.StrUtils;

import lombok.AllArgsConstructor;

@RestController
@RequestMapping("auth/api")
@AllArgsConstructor
public class AuthApiController {

	private final AuthApiService ApiService;
	private final AuthTokenService ts;

	@Auth
	@PostMapping(value = "list")
	public Res list(@RequestHeader("token") String token) {
		List<AuthApi> users = ApiService.list();
		Res res = Res.success(null, users, users.size());
		return ts.getToken(res, token);
	}

	@Auth
	@PostMapping(value = "save")
	public Res save(@RequestBody AuthApi api, @RequestHeader("token") String token) {
		if (StrUtils.isEmpty(api.getId()))
			ApiService.insert(api);
		else
			ApiService.update(api);
		Res res = Res.success("保存成功");
		return ts.getToken(res, token);
	}

	@Auth
	@PostMapping(value = "remove")
	public Res remove(@RequestBody AuthApi api, @RequestHeader("token") String token) {
		ApiService.remove(api);
		Res res = Res.success("删除成功");
		return ts.getToken(res, token);
	}

}
